import { Router } from 'express';
import { PostController } from '../controllers/PostController';

const router: Router = Router();
const postController = new PostController();

/**
 * @swagger
 * components:
 *   schemas:
 *     Post:
 *       type: object
 *       required:
 *         - title
 *         - content
 *         - authorId
 *       properties:
 *         id:
 *           type: integer
 *           description: 文章ID
 *         title:
 *           type: string
 *           minLength: 5
 *           description: 文章标题
 *         content:
 *           type: string
 *           description: 文章内容
 *         summary:
 *           type: string
 *           description: 文章摘要
 *         published:
 *           type: boolean
 *           description: 是否已发布
 *         publishedAt:
 *           type: string
 *           format: date-time
 *           description: 发布时间
 *         authorId:
 *           type: integer
 *           description: 作者ID
 *         createdAt:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 *         updatedAt:
 *           type: string
 *           format: date-time
 *           description: 更新时间
 *         author:
 *           $ref: '#/components/schemas/User'
 *     CreatePostInput:
 *       type: object
 *       required:
 *         - title
 *         - content
 *         - authorId
 *       properties:
 *         title:
 *           type: string
 *           minLength: 5
 *           description: 文章标题
 *         content:
 *           type: string
 *           description: 文章内容
 *         summary:
 *           type: string
 *           description: 文章摘要
 *         authorId:
 *           type: integer
 *           description: 作者ID
 *     UpdatePostInput:
 *       type: object
 *       properties:
 *         title:
 *           type: string
 *           minLength: 5
 *           description: 文章标题
 *         content:
 *           type: string
 *           description: 文章内容
 *         summary:
 *           type: string
 *           description: 文章摘要
 *     PaginatedPosts:
 *       type: object
 *       properties:
 *         posts:
 *           type: array
 *           items:
 *             $ref: '#/components/schemas/Post'
 *         pagination:
 *           type: object
 *           properties:
 *             page:
 *               type: integer
 *               description: 当前页码
 *             limit:
 *               type: integer
 *               description: 每页数量
 *             total:
 *               type: integer
 *               description: 总数
 *             totalPages:
 *               type: integer
 *               description: 总页数
 */

/**
 * @swagger
 * /api/posts:
 *   post:
 *     tags: [Posts]
 *     summary: 创建文章
 *     description: 创建一篇新文章
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/CreatePostInput'
 *     responses:
 *       201:
 *         description: 文章创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: 文章创建成功
 *                 post:
 *                   $ref: '#/components/schemas/Post'
 *       400:
 *         description: 请求错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.post('/', postController.createPost);

/**
 * @swagger
 * /api/posts:
 *   get:
 *     tags: [Posts]
 *     summary: 获取文章列表
 *     description: 获取分页的文章列表，可按发布状态筛选
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: published
 *         schema:
 *           type: boolean
 *         description: 筛选已发布/未发布的文章
 *     responses:
 *       200:
 *         description: 成功获取文章列表
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/PaginatedPosts'
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.get('/', postController.getAllPosts);

/**
 * @swagger
 * /api/posts/{id}:
 *   get:
 *     tags: [Posts]
 *     summary: 获取文章详情
 *     description: 根据ID获取单篇文章的详细信息
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 文章ID
 *     responses:
 *       200:
 *         description: 成功获取文章信息
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 post:
 *                   $ref: '#/components/schemas/Post'
 *       404:
 *         description: 文章不存在
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 文章不存在
 */
router.get('/:id', postController.getPostById);

/**
 * @swagger
 * /api/posts/{id}:
 *   put:
 *     tags: [Posts]
 *     summary: 更新文章
 *     description: 更新指定文章的信息
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 文章ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: '#/components/schemas/UpdatePostInput'
 *     responses:
 *       200:
 *         description: 文章更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: 文章更新成功
 *                 post:
 *                   $ref: '#/components/schemas/Post'
 *       404:
 *         description: 文章不存在
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 文章不存在
 */
router.put('/:id', postController.updatePost);

/**
 * @swagger
 * /api/posts/{id}:
 *   delete:
 *     tags: [Posts]
 *     summary: 删除文章
 *     description: 删除指定的文章
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 文章ID
 *     responses:
 *       200:
 *         description: 文章删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: 文章删除成功
 *       404:
 *         description: 文章不存在
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 文章不存在
 */
router.delete('/:id', postController.deletePost);

/**
 * @swagger
 * /api/posts/{id}/publish:
 *   patch:
 *     tags: [Posts]
 *     summary: 发布文章
 *     description: 将文章状态设置为已发布
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 文章ID
 *     responses:
 *       200:
 *         description: 文章发布成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: 文章发布成功
 *                 post:
 *                   $ref: '#/components/schemas/Post'
 *       404:
 *         description: 文章不存在
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 文章不存在
 */
router.patch('/:id/publish', postController.publishPost);

/**
 * @swagger
 * /api/posts/{id}/unpublish:
 *   patch:
 *     tags: [Posts]
 *     summary: 取消发布文章
 *     description: 将文章状态设置为未发布
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 文章ID
 *     responses:
 *       200:
 *         description: 取消发布成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: 文章已取消发布
 *                 post:
 *                   $ref: '#/components/schemas/Post'
 *       404:
 *         description: 文章不存在
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 error:
 *                   type: string
 *                   example: 文章不存在
 */
router.patch('/:id/unpublish', postController.unpublishPost);

/**
 * @swagger
 * /api/posts/author/{authorId}:
 *   get:
 *     tags: [Posts]
 *     summary: 获取作者的文章
 *     description: 获取指定作者的所有文章
 *     parameters:
 *       - in: path
 *         name: authorId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 作者ID
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: published
 *         schema:
 *           type: boolean
 *         description: 筛选已发布/未发布的文章
 *     responses:
 *       200:
 *         description: 成功获取作者文章列表
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/PaginatedPosts'
 *       500:
 *         description: 服务器错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/Error'
 */
router.get('/author/:authorId', postController.getPostsByAuthor);

export default router;